% ========================================================================
% Output that must be produced by each code
% -----------------------------------------
% XESTMODE:  Estimated value
% ESTIMITER: Number of Iterations
% EXITFLAG : How routine ended
% LPOSTMIN : Exit value of posterior (min)
% April 19 2010
% ========================================================================
try
    funcname=@lmj_postoptim_central; 
    switch case_estima
        case 1
            
            %[lpostmin,xestmode,estimiter,exitflag]=lmj_postoptim_csminwel(xzero,partrmat_est,parvecmode,parposest,estimopt,funcmod,Y,trainvec,prpar,prss,solveopt,addsol,ssposest); 
            
            % =================================================================
            % 1. CSMINWEL
            % IO: transform from MOD 2 MIN and viceversa
            % =================================================================
            xzeromin          = mod2min(xzero,partrmat_est);
            
            [lpostmin,xestmin,~,H,estimiter,junk,exitflag]=...
                csminwel(funcname,xzeromin, 0.25*eye(length(xzeromin)),[],estimopt.TolFun,estimopt.MaxIter, ...
                parvecmode,parposest,partrmat_est,funcmod,Y,trainvec,prpar,prss,solveopt,addsol,ssposest,1);
            
                
            xestmode  = min2mod(xestmin,partrmat_est);
            
        case 2 % FMINCON
            
            % =================================================================
            % 2. FMINCON
            % ESTIMA_FMINCON.m
            % IO No transformation needed
            % Bounds transformed inside code
            % =================================================================
            [lpostmin,xestmode,exitflag,fminconout,Hx]=estima_fmincon_central(xzero,parvecmode,parposest,funcmod,Y,trainvec,prpar,prss,solveopt,addsol,ssposest,estimopt);
            estimiter = fminconout.iterations;
            % Old way of calling FMINCON; use subfunction directly
            %             flag_transform = 0;
            %             [xestmode, junk, exitflag, fminconout] = fmincon(@(x)lmj_postoptim(x,parstrval,parposest, ...
            %                 partrmat_est, funcmod, Y, trainvec, prpar, prss, solveopt, addsol, ssposest, flag_transform), ...
            %                 x0mode,[],[],[],[],prpar_lb(parposest),prpar_ub(parposest),[], estimopt);
            %             estimiter = fminconout.iterations;
            % [xestmode, junk, exitflag, fminconout] = fmincon(estfunc, x0mode,[],[],[],[],prpar_lb(parposest),prpar_ub(parposest),[], estimopt);
        case 3      % Giorgio's SA
            % =================================================================
            % 3. Simple version of SA
            % IO: transform from MOD 2 MIN and viceversa
            % ================================================================
            xzeromin          = mod2min(xzero,partrmat_est);
            
            nest=4*round(estimopt.MaxIter/4);
            dispaj('Running SA for ',nest,' iterations');
            [fh,xestmin,estimiter,exitflag,fvec]=simulated_simp(@lmj_postoptim,xzeromin(:) ...
                ,2,1e5,nest,parvecmode,parposest,partrmat_est,funcmod,Y,trainvec,prpar,prss,solveopt,addsol,ssposest,1);
            
            xestmode  = min2mod(xestmin,partrmat_est);
            
        case 4
            % =================================================================
            % 4. SIMMAN
            % IO: No transformation needed, except bounds passed from
            %     outside
            % ================================================================
            
            [xestmode, exitflag, estimiter, fvec]=simann(@(x)lmj_postoptim(x, parvecmode, parposest, ...
                partrmat_est, funcmod, Y, trainvec, prpar, prss, solveopt, addsol, ssposest, 0), ...
                xzero, prpar_lb(parposest),prpar_ub(parposest), estimopt);
            
        case 5
            
            % =================================================================
            % 4. FMINSEARCH
            % ESTIMA_FMINCON.m
            % IO: No transformation needed, bounds truncated inside code
            %     outside
            % =============================================================
            [lpostmin,xestmode,exitflag,fminsearchout]=estima_fminsearch(xzero,parvecmode,parposest,partrmat_est,funcmod,Y,trainvec,prpar,prss,solveopt,addsol,ssposest,estimopt);
            flag_transform = 1;
            estimiter = fminsearchout.iterations;
            
    end % END SWITCH
    
catch
    
    errcount=errcount+1;
    flag_optimok=0;
    
    try
        cd(outpath);
        diary errors;
        disp('Error in estima_loop');
        dispaj('Iteration Number ',ii);
        lasterr
        diary off;
        cd(cucd); 
    catch
        cd(backup_path);
        diary errors;
        disp('Error in estima_loop');
        dispaj('Iteration Number ',ii);
        lasterr
        diary off;
        cd(cucd);
    end % END TRY CATCH IN
    
end %END TRY CATCH OUT

try 
cd(cucd); 
cd(outpath); 
save workspace; 
cd(cucd); 
catch 
    disp('Could not save WORKSPACE to drive!') 
end 

